package fr.cdesire.sudoku.applet;

import netscape.javascript.JSObject;
import fr.cdesire.sudoku.applet.action.SudokuEventEnum;
import fr.cdesire.sudoku.applet.applet.MessageAnimeApplet;
import fr.cdesire.sudoku.applet.exception.SudokuAppletException;
import fr.cdesire.sudoku.applet.exception.TechnicalException;
import fr.cdesire.sudoku.applet.utils.GrilleManager;

/**
 * Cette classe affiche la solution d'une grille Sudoku.
 * 
 * @author cedric.desire@gmail.com
 */
public class SudokuApplet extends MessageAnimeApplet {

	/**
	 * Serial ID
	 */
	private static final long serialVersionUID = -2320936838920656826L;

	private JSObject win = null;
	private GrilleManager grilleManager = null;
	
	@Override
	public void init() {
		super.init();
		win = JSObject.getWindow(this);
		grilleManager = GrilleManager.getInstance();
		updateMessageAnime(grilleManager.getMessage());
	}

	public void actionPerformed(String event, String json) {
		
		try {
			executeAction(SudokuEventEnum.getSudokuEvent(event), json);
			updateMessageAnime(grilleManager.getMessage());
			win.call("setGrille", new Object[] { grilleManager.toString() });
		} catch (SudokuAppletException e) {
			updateMessageAnime(grilleManager.getMessage());
		}
	}
	
	protected void executeAction(SudokuEventEnum event, String json) throws TechnicalException, SudokuAppletException {
		event.getAction().execute(json);
	}
}
